package com.szcinda.controller.excelUtil;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

public class MyMergeStrategy extends AbstractMergeStrategy {
    private List<Integer> groupCount;
    private Sheet sheet;

    public MyMergeStrategy(List<Integer> groupCount) {
        this.groupCount = groupCount;
    }

    // 按照分组将各种类别分别合并成一个单元格
    private void mergeGroupColumn(Integer index) {
        int rowCnt = 1;
        for (Integer count : groupCount) {
            CellRangeAddress cellRangeAddress = new CellRangeAddress(rowCnt + 1, rowCnt + count, index, index);
            sheet.addMergedRegionUnsafe(cellRangeAddress);
            rowCnt += count;
        }
    }


    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer integer) {
        this.sheet = sheet;
        if (cell.getRowIndex() == 7) {
            switch (cell.getColumnIndex()) {
                case 0:
                    this.mergeGroupColumn(0);
                    break;
                case 1:
                    this.mergeGroupColumn(1);
                    break;
                case 2:
                    this.mergeGroupColumn(2);
                    break;
                case 3:
                    this.mergeGroupColumn(3);
                    break;
                case 4:
                    this.mergeGroupColumn(4);
                    break;
                case 5:
                    this.mergeGroupColumn(5);
                    break;
                case 13:
                    this.mergeGroupColumn(13);
                    break;
                default:
                    break;
            }
        }
    }
}
